package com.example.demo.handle;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;

public class HandleImage {
/*    public static void createSmallPhoto() throws IOException {
        File _file = new File("C:\\Users\\wuzhengya\\Desktop\\领红包\\guoqi.png");
        Image src;
        src = javax.imageio.ImageIO.read(_file);
        int wideth = 110;
        int height = 80;
        BufferedImage tag = new BufferedImage(wideth, height, BufferedImage.TYPE_INT_RGB);
        tag.getGraphics().drawImage(src, 0, 0, wideth, height, null);
        FileOutputStream out = new FileOutputStream("D:\\新建文件夹\\1.2.png");
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
        encoder.encode(tag);
        out.close();
    }*/
    public static boolean changeSize(int newWidth, int newHeight, String path,String formatName) {
        BufferedInputStream in = null;
        try {
            in = new BufferedInputStream(new FileInputStream(path));
            //字节流转图片对象
            Image bi = ImageIO.read(in);
            //构建图片流
            BufferedImage tag = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
            //绘制改变尺寸后的图
            tag.getGraphics().drawImage(bi, 0, 0, newWidth, newHeight, null);
            //输出流
            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(path));
            //JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
            //encoder.encode(tag);
            ImageIO.write(tag, formatName, out);
            in.close();
            out.close();
            return true;
        } catch (IOException e) {
            return false;
        }
    }

    public static boolean changeSize2(int newWidth, int newHeight, String path,String formatName) {
        BufferedInputStream in = null;
        try {
            in = new BufferedInputStream(new FileInputStream(path));
            //字节流转图片对象
            Image bi = ImageIO.read(in);
            //构建图片流
            BufferedImage tag = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
            //绘制改变尺寸后的图
//            tag.getGraphics().drawImage(bi, 0, 0, newWidth, newHeight, null);

            Rectangle rect = new Rectangle(100, 100, 400, 600);
            //输出流
            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(path));
            //JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
            //encoder.encode(tag);
            ImageIO.write(tag, formatName, out);
            in.close();
            out.close();
            return true;
        } catch (IOException e) {
            return false;
        }
    }


}
